package com.yqz.bussi.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.yqz.bussi.form.BussiBookingCustomerForm;
import com.yqz.bussi.form.BussiPaymentRecordForm;
import com.yqz.bussi.mapper.BussiBookingCustomerMapper;
import com.yqz.bussi.mapper.BussiPaymentRecordMapper;
import com.yqz.bussi.service.BussiPaymentRecordService;
import com.yqz.bussi.vo.BussiPaymentRecordVO;
import com.yqz.common.CodeMsg;
import com.yqz.common.Constant;
import com.yqz.common.Result;
import com.yqz.common.base.service.impl.BaseServiceImpl;
import com.yqz.common.exception.BussiException;
import com.yqz.common.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.Map;

@Service
public class BussiPaymentRecordServiceImpl extends BaseServiceImpl implements BussiPaymentRecordService {

    @Autowired
    private BussiPaymentRecordMapper bussiPaymentRecordMapper;

    @Autowired
    private BussiBookingCustomerMapper bussiBookingCustomerMapper;

    public BussiPaymentRecordServiceImpl(BussiPaymentRecordMapper bussiPaymentRecordMapper) {
        super(bussiPaymentRecordMapper);
    }

    @Override
    @Transactional
    public Result addPayMent(MultipartFile image, BussiPaymentRecordForm bussiPaymentRecordForm) {
        Integer type = bussiPaymentRecordForm.getType();
        if(type != null && type.equals(Constant.PAYMENT_TYPE_SERVICE)){
            String serviceBeginTime = bussiPaymentRecordForm.getServiceBeginTime();
            String serviceEndTime = bussiPaymentRecordForm.getServiceEndTime();
            if(StrUtil.isEmpty(serviceBeginTime) || StrUtil.isEmpty(serviceEndTime)){
                return new Result(CodeMsg.PAYMENT_SERVICE_TIME_ERROR);
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("serviceBeginTime",serviceBeginTime);
            jsonObject.put("serviceEndTime",serviceEndTime);
            bussiPaymentRecordForm.setExtData(jsonObject.toJSONString());
        }
        bussiPaymentRecordForm.setState(Constant.PAYMENT_STATE_CHECK_PENDING);
        bussiPaymentRecordForm.setUserId(WebUtil.getCurrentCompUser().getId());
        bussiPaymentRecordForm.setUserName(WebUtil.getCurrentCompUser().getName());
        bussiPaymentRecordForm.setCreateTime(DateUtil.format(new Date(),Constant.YYYY_MM_DD_HHMMSS));
        String imgName = DateUtil.format(new Date(), Constant.YYYYMMDDHHMMSSSSS);
        String originalFilename = image.getOriginalFilename();
        String fileSuffix = FileUtil.extName(originalFilename);
        imgName = imgName + "." + fileSuffix;
        bussiPaymentRecordForm.setPayImg(Constant.IMG_FOLDER_NAME + "/" + imgName);
        super.add(bussiPaymentRecordForm);
        File file = new File(Constant.IMG_FOLDER_PATH, imgName);
        try {
            image.transferTo(file);
        } catch (Exception e) {
            throw new BussiException(CodeMsg.PAYMENT_ADD_ERROR.CODE,CodeMsg.PAYMENT_ADD_ERROR.MSG);
        }
        return new Result();
    }

    @Override
    @Transactional
    public Result updateState(Integer id, Integer state) {
        BussiPaymentRecordVO bussiPaymentRecordVO = bussiPaymentRecordMapper.selectByPrimaryKey(id);
        if(bussiPaymentRecordVO == null){
            throw new BussiException(CodeMsg.PAYMENT_RECORD_ID_ERROR.CODE,CodeMsg.PAYMENT_RECORD_ID_ERROR.MSG);
        }
        if(bussiPaymentRecordVO.getState() != Constant.PAYMENT_STATE_CHECK_PENDING){
            throw new BussiException(CodeMsg.PAYMENT_CHECKED_ERROR.CODE,CodeMsg.PAYMENT_CHECKED_ERROR.MSG);
        }
        bussiPaymentRecordMapper.updateStateByKey(id,state);
        if(state.equals(Constant.PAYMENT_STATE_CHECK_FAILD)){
            return new Result();
        }
        Integer type = bussiPaymentRecordVO.getType();
        if(type.equals(Constant.PAYMENT_TYPE_SERVICE)){
            return doCompServiceTime(bussiPaymentRecordVO);
        } else if (type.equals(Constant.PAYMENT_TYPE_CHECK)){
            return doCompCheckState(bussiPaymentRecordVO);
        }
        return new Result();
    }

    @Override
    public Result queryUnCheckCount() {
        Map<String,Integer> data = bussiPaymentRecordMapper.selectCountByState(Constant.PAYMENT_STATE_CHECK_PENDING);
        return new Result(data);
    }

    private Result doCompServiceTime(BussiPaymentRecordVO bussiPaymentRecordVO) {
        Integer custId = bussiPaymentRecordVO.getCustId();
        BussiBookingCustomerForm form = new BussiBookingCustomerForm();
        String extData = bussiPaymentRecordVO.getExtData();
        JSONObject jsonObject = JSONObject.parseObject(extData);
        String serviceEndTime = jsonObject.getString("serviceEndTime");
        bussiBookingCustomerMapper.updateServiceEndTime(custId,serviceEndTime);
        return new Result();
    }

    private Result doCompCheckState(BussiPaymentRecordVO bussiPaymentRecordVO) {
        Integer custId = bussiPaymentRecordVO.getCustId();
        bussiBookingCustomerMapper.updateCheckFeeState(custId,Constant.CUSTOMER_CHECKED_FEE_STATE_VALID);
        return new Result();
    }


}
